package org.quasar.airNova.presentationLayer;


import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import org.quasar.airNova.businessLayer.CalendarDate;
import org.quasar.airNova.businessLayer.Instant;
import org.quasar.airNova.persistenceLayer.Database;

public class Panel extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
									
					Panel frame = new Panel();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Panel() {
		Database.open("database", "AirNova", "db4o");
		inserirDadosIniBD();
		setTitle("Op\u00E7\u00F5es");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnVoo = new JMenu("Voo ");
		menuBar.add(mnVoo);
		
		JMenuItem mntmCriarVoo = new JMenuItem("Criar Voo");
		mntmCriarVoo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new CriarVoo();
			}
		});
		mnVoo.add(mntmCriarVoo);
		
		JMenuItem mntmAlterarVoo = new JMenuItem("Alterar Voo");
		mntmAlterarVoo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new AlterarVoo();
			}
		});
		mnVoo.add(mntmAlterarVoo);
		
		JMenuItem mntmCancelarVoo = new JMenuItem("Cancelar Voo");
		mntmCancelarVoo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new CancelarVoo();
			}
		});
		mnVoo.add(mntmCancelarVoo);
		
//		JMenu mnBilhete = new JMenu("Bilhete");
//		menuBar.add(mnBilhete);
//		
//		JMenuItem mntmComprarBilhete = new JMenuItem("Comprar Bilhete");
//		mntmComprarBilhete.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				new ComprarBilhete();
//							}
//		});
//		mnBilhete.add(mntmComprarBilhete);
//		
//		JMenuItem mntmCancelarBilhete = new JMenuItem("Cancelar Bilhete");
//		mntmCancelarBilhete.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				new CancelarBilhete();
//			}
//		});
//		mnBilhete.add(mntmCancelarBilhete);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
	}
	private void inserirDadosIniBD() {
		CalendarDate today = new CalendarDate(8,2,null,2012);
		Database.insert(today);
		System.out.println(today.toString());
				
		Instant instante = new Instant( 12,  30,  25);
		Instant instante2 = new Instant( 15,  30,  25);
		Database.insert(instante);
		Database.insert(instante2);
		Database.close();
		
	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
